<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles, assignRoles } from '@/api/employees'
import { getUserDetailById } from '@/api/user'

export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 选中状态
      list: []

    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 给用户分配角色
    hSubmit() {
      try {
        const res = assignRoles({ id: this.curId, roleIds: this.roleIds })
        console.log('给用户分配角色', res)
        this.loadRoles()
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取用户角色信息
    async loadRoles() {
      try {
        const res = await getRoles()
        // 获取所有的角色信息
        console.log('getRoles', res)
        // 保存数据
        this.list = res.data.rows
        // 获取当前员工的角色
        const info = await getUserDetailById(this.curId)
        console.log('info', info)
        this.roleIds = info.data.roleIds // 保存这个员工当前的已经有的角色
      } catch (err) {
        console.log('getRoles', err)
      }
    }
  }
}
</script>
